﻿Public Class Wfrm_Busca_Persona
    Inherits Wfrm_Master

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            CargarSociedades()
            Carga_UORG()
            Carga_UNeg()
            Carga_CARGOS()

        End If
    End Sub

    Sub Carga_CARGOS()

        Dim rp As New DotNetResponse.SQLPersistence
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        rp.StoredProcedure = "SP_GET_UP_CARGOS"
        rp.AddParametro(cmb_sociedad.SelectedValue)
        exec.ExecuteDataTable(rp)
        cmb_cargo.DataSource = rp.DtTable
        cmb_cargo.DataTextField = "nombre_cargo"
        cmb_cargo.DataValueField = "id_cargo"
        cmb_cargo.DataBind()
        cmb_cargo.UpdateAfterCallBack = True

        Dim ITEM As New ListItem
        ITEM.Text = "TODOS"
        ITEM.Value = -1
        ITEM.Selected = True
        cmb_cargo.Items.Add(ITEM)

        If rp.Errores Then
            MsgError(rp.MensajeError)
        End If

    End Sub
    Sub Carga_UORG()        
        Dim rp As New DotNetResponse.SQLPersistence
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        'Dim comited As Boolean = Conexion.Conectar(exec)
        rp.StoredProcedure = "SP_GET_UP_UNID_ORG"
        rp.AddParametro(cmb_sociedad.SelectedValue)
        exec.ExecuteDataTable(rp)      
        cmb_unid_org.DataSource = rp.DtTable
        cmb_unid_org.DataTextField = "NOMBRE_UORG"
        cmb_unid_org.DataValueField = "ID_UORG"
        cmb_unid_org.DataBind()
        cmb_unid_org.UpdateAfterCallBack = True

        Dim ITEM As New ListItem
        ITEM.Text = "TODOS"
        ITEM.Value = -1
        ITEM.Selected = True
        cmb_unid_org.Items.Add(ITEM)


        If rp.Errores Then
            MsgError(rp.MensajeError)
        End If
    End Sub
    Sub Carga_UNeg()
        Dim rp As New DotNetResponse.SQLPersistence
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        'Dim comited As Boolean = Conexion.Conectar(exec)
        rp.StoredProcedure = "SP_GET_UP_UNID_NEG"
        rp.AddParametro(cmb_sociedad.SelectedValue)
        exec.ExecuteDataTable(rp)
        cmb_unid_neg.DataSource = rp.DtTable
        cmb_unid_neg.DataTextField = "Nombre_UnidadNegocio"
        cmb_unid_neg.DataValueField = "Id_UnidadNegocio"
        cmb_unid_neg.DataBind()
        cmb_unid_neg.UpdateAfterCallBack = True

        Dim ITEM As New ListItem
        ITEM.Text = "TODOS"
        ITEM.Value = -1
        ITEM.Selected = True
        cmb_unid_neg.Items.Add(ITEM)

        If rp.Errores Then
            MsgError(rp.MensajeError) '
        End If
    End Sub
    Sub CargarSociedades()
        Dim rp As New DotNetResponse.SQLPersistence
        cmb_sociedad.DataSource = Database.SOCIEDAD.GET_00(rp)
        cmb_sociedad.DataTextField = "razon_social_sociedad"
        cmb_sociedad.DataValueField = "id_sociedad"
        cmb_sociedad.DataBind()

        If rp.Errores Then
            MsgError(rp.MensajeError)
        End If
    End Sub

    Protected Sub cmb_sociedad_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmb_sociedad.SelectedIndexChanged
        Carga_UORG()
        Carga_UNeg()
        Carga_CARGOS()
    End Sub

    Protected Sub btn_buscar_Click(sender As Object, e As EventArgs) Handles btn_buscar.Click

        '      ()
        ' @CI AS VARCHAR(30)
        ',@PATERNO AS VARCHAR(30)
        ',@MATERNO AS VARCHAR(30)
        ',@UORG INT
        ',@UNEG INT
        ',@CARGO INT  

        Dim rp As New DotNetResponse.SQLPersistence
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        'Dim comited As Boolean = Conexion.Conectar(exec)
        rp.StoredProcedure = "SP_GET_UP_BUSCA_PERSONA"
        If txt_rut.Text.Trim = "" Then
            rp.AddParametro(Nothing)
        Else
            ' If DotNetDal.Generica.ValidaRut(txt_rut.Text) Then
            If InStr(txt_rut.Text, "-") > 0 Then
                txt_rut.Text = Mid(txt_rut.Text, 1, InStr(txt_rut.Text, "-") - 1)
            End If
            Dim rt As String = txt_rut.Text.Replace(".", "").Replace("-", "")
            rp.AddParametro(rt)
            'rp.AddParametro(Mid(rt, 1, Len(rt) - 1))
            'Else
            'MsgError("Rut inválido")
            'Exit Sub
            'End If
        End If

        If txt_paterno.Text.Trim = "" Then
            rp.AddParametro(Nothing)
        Else
            rp.AddParametro(txt_paterno.Text.Trim)
        End If

        If txt_materno.Text.Trim = "" Then
            rp.AddParametro(Nothing)
        Else
            rp.AddParametro(txt_materno.Text.Trim)
        End If

        If cmb_unid_org.SelectedValue = "-1" Then
            rp.AddParametro(Nothing)
        Else
            rp.AddParametro(cmb_unid_org.SelectedValue)
        End If

        If cmb_unid_neg.SelectedValue = "-1" Then
            rp.AddParametro(Nothing)
        Else
            rp.AddParametro(cmb_unid_neg.SelectedValue)
        End If

        If cmb_cargo.SelectedValue = "-1" Then
            rp.AddParametro(Nothing)
        Else
            rp.AddParametro(cmb_cargo.SelectedValue)
        End If

        exec.ExecuteDataTable(rp)

        DG_Resultado.DataSource = rp.DtTable
        DG_Resultado.DataBind()
        DG_Resultado.UpdateAfterCallBack = True

        If Not rp.TieneDatos Then
            MsgInformativo("No se ha encontrado información")
        End If

    End Sub

    Private Sub DG_Resultado_PageIndexChanged(source As Object, e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DG_Resultado.PageIndexChanged
        btn_buscar_Click(Nothing, Nothing)

        DG_Resultado.CurrentPageIndex = e.NewPageIndex
        DG_Resultado.DataBind()
        DG_Resultado.UpdateAfterCallBack = True

    End Sub

    Protected Sub DG_Resultado_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DG_Resultado.SelectedIndexChanged
        Response.Redirect("wfrm_inf_basica_cliente.aspx?id=" & DG_Resultado.SelectedItem.Cells(1).Text)
    End Sub
End Class